Python Basic

Python is a widely used general-purpose, high level programming language. It was initially designed by Guido van Rossum in 1991 and developed by Python Software Foundation. It was mainly developed for emphasis on code readability, and its syntax allows programmers to express concepts in fewer lines of code.  There are two major Python versions- Python 2 and Python 3. Both are quite different.

Python syntax can be executed by writing directly in the Command Line:

print("Hello, World!")


Or by creating a python file on the server, using the .py file extension, and running it in the Command Line:

Python Data Types

There are three numeric types in Python:
  • int
  • float
  • complex
Variables of numeric types are created when you assign a value to them:
x = 1  # int
y = 2.8 # float
z = 1j   # complex

To verify the type of any object in Python, use the type() function.
print(type(x))
print(type(y))
print(type(z))


Comments in Python
There are two types of comments in Python.
  • Single line comment
  • Multiple line comment
Single Line Comment
In python we use # special character to start the comment. Lets take few examples to understand the usage.

# This is just a comment. Anything written here is ignored by Python

Muli-line comment
To have a multi-line comment in Python, we use triple single quotes at the beginning and at the end of the comment, as shown below.

'''
This is a
multi-line
comment
'''
Reading Input
The input() function reads a line from input, converts it to a string (stripping a trailing newline), and returns that. The function takes an optional argument, which is written to standard output without a trailing newline, if present.

name = input("Enter your name:")
print("Hello", name)

How memory management is done in Python?
  • In Python memory management is done using private heap space. The private heap is the storage area for all the data structures and objects. The interpreter has access to the private heap and the programmer cannot access this private heap. 
  • The storage allocation for the data structures and objects in Python is done by the memory manager. The access for some tools is provided by core API for programmers to code.
  • The built-in garbage collector in Python is used to recycle all the unused memory so that it can be available for heap storage area.

No comments:

Post a Comment